import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import LoginForm from './components/LoginForm.jsx';
// import { setValue } from '../../utils/localStorageApi.js';
import './index.less';

class Login extends Component {
  state = {
    loading: false,
    errorMsg: ''
  }

  login = (params) => {
    const { userLogin, system, updateLoginUserInfo, history } = this.props;
    this.setState({
      loading: true
    });

    this.setState({
      loading: false
    });
    const pathname = sessionStorage.getItem('pathname');
    if (pathname) {
      history.push(pathname);
    } else{
      history.push('/');
    }

    // userLogin(params).then(() => {
    //   const { loginStore } = this.props;
    //   const loginUser = loginStore.login.get('loginData');
    //   const loginMessage = loginStore.login.get('loginMessage');
    //   if (loginUser) {
    //     setValue('userInfo', loginUser.toJS());
    //     this.setState({
    //       loading: false
    //     });
    //     let pathname = sessionStorage.getItem('pathname');
    //     if (pathname) {
    //       history.push(pathname);
    //     } else{
    //       history.push('/');
    //     }
    //   } else {
    //     this.setState({
    //       errorMsg: loginMessage,
    //       loading: false
    //     });
    //   }

    // }).catch(err => {
    //   this.setState({
    //     loading: false
    //   });
    //   throw new Error(err);
    // });
  }

  render() {
    const { errorMsg, loading } = this.state;

    return (
      <div className="login-page" onKeyPress={e => {
        if (e.which === 13 && e.target.type === 'password') {
          this.LoginForm.login();
        }
      }}>
        <div className="login-header">
          <div className="logo">
            后台管理系统
          </div>
        </div>
        <div className="login-content-wrap">
          <div className="login-content">
            <div className="word"></div>
            <div className="login-box">
              <div className="title">账号登录</div>
              <LoginForm wrappedComponentRef={inst => { this.LoginForm = inst; }} onLogin={this.login} errorMsg={errorMsg} loading={loading}/>
            </div>
          </div>
        </div>
        <footer>
          <span>Copyright ©️ 2018 zp Inc. All rights reserved.京ICP备12039470号</span>
        </footer>
      </div>
    );
  }
}

export default withRouter(Login);